// pc13电灯闪烁，使用定时器
// #![deny(unsafe_code)] //可选
// necessary
#![no_std]
#![no_main]

use panic_halt as _;

use nb::block;

use cortex_m_rt::entry;
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};

#[entry]
fn main() -> ! {
    // 内核外设
    let cp = cortex_m::Peripherals::take().unwrap();

    // 硬件外设
    let dp = pac::Peripherals::take().unwrap();

    // 初始化系统时钟,8Mhz,内部低速晶振
    let rcc = dp.RCC.constrain();
    let mut flash = dp.FLASH.constrain();
    let clocks = rcc.cfgr.freeze(&mut flash.acr);
    // 初始化GPIOC
    let mut gpioc = dp.GPIOC.split();
    // 初始化PC13为输出
    // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
    // in order to configure the port. For pins 0-7, crl should be passed instead.
    let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
    // 初始化定时器
    let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
    // 开启定时器
    timer.start(1.Hz()).unwrap();

    let mut num1:u8 = 1;
    let mut num2:u8 = 2;
    loop {
        // 等待定时器溢出
        block!(timer.wait()).unwrap();
        // 点亮LED
        led.set_high();
        num1 +=2;
        // 等待定时器溢出
        block!(timer.wait()).unwrap();
        // 熄灭LED
        led.set_low();
        num2 +=4;
    }
}